<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<script src="../script/mui.min.js"></script>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../css/app.css" />
		<style type="text/css">
			body {
				color: #3E9FEC;
				background-color: #FFF;
			}
			.mui-table-view-cell:after {
				background-color: #E2E2E4
			}
			.mui-bar-nav {
				-webkit-box-shadow: none;
				box-shadow: none;
			}
			.mui-bar {
				-webkit-box-shadow: none;
				box-shadow: none;
			}
			.wrong {
				color: #e02a53
			}
			.undo {
				color: #4fd2c2;
			}
			.item-col {
				width: 20%;
				float: left;
				margin-top: 15px;
				margin-bottom: 10px;
			}
			.item {
				background-color: #f0f0f0;
				width: 32px;
				height: 32px;
				font-size: 14px;
				line-height: 32px;
				text-align: center;
				border-radius: 16px;
				margin: 0px auto;
			}
			.itempad {
				/*margin-top: 80px;*/
				
				padding: 20px 50px;
			}
			.itempad p {
				color: #fff;
				font-size: 15px;
			}
			.items-row {} .mui-btn-positive {
				background-color: #4fd2c2;
				border: 1px solid #4fd2c2;
			}
			.itempad2 .items-row {
				padding: 50px 50px;
				background-color: #fff;
			}
			.itempad2 .items-row p {
				font-size: 15px;
				color: #30A4ED;
				text-align: center;
			}
			#footer {
				color: #fff;
			}
			#newcourse{
				visibility: hidden;
			}
			#content{
				padding-bottom: 50px;
			}
		</style>
	</head>

	<body>
		<div class="mui-content" id="content">

		</div>

		<nav class="mui-bar mui-bar-tab" id="footer">
			<div id="newcourse">
				<div class="mui-pull-left" style="height: 55px;width:50%;background-color: #30a4ed;" id="begin">
					<span style="line-height: 55px;display: block;" class="mui-text-center">开始做题</span>
				</div>
				<div class="mui-pull-left" style="height: 55px;width:50%;background-color: #4fd2c2;display: none;" id="buy">
					<span style="line-height: 55px;display: block;" class="mui-text-center" id="buytext">购买课程</span>
				</div>
				<div class="mui-pull-left" style="height: 55px;width:50%;background-color: #4fd2c2;display: none;" id="purchased">
					<span style="line-height: 55px;display: block;" class="mui-text-center">已购买</span>
				</div>
				<div class="mui-pull-left" style="height: 55px;width:50%;background-color: #30a4ed;display: none;" id="wrong">
					<span style="line-height: 55px;display: block;" class="mui-text-center">错题解析</span>
				</div>
			</div>
		</nav>

		<script type="text/html" id="contentTem">
			<ul class="mui-table-view">
				<li class="mui-table-view-cell">
					<h4>试卷名称:</h4>
					<p id="title">{{#name}}</p>
				</li>
				<li class="mui-table-view-cell">
					<h4>试卷详情:</h4>
					<p>{{#description}}</p>
					<p>时长：{{if limitedtime=="0"}}无限制{{else}}{{limitedtime}}分钟{{/if}}</p>
					<p>总分：{{score}}分</p>
				</li>
				<li class="mui-table-view-cell">
					<h4>试卷组成:</h4> {{each $toObj(metas).question_type_seq as v i }} {{if v=="single_choice"}}
					<p>单选题</p>{{/if}} {{if v=="uncertain_choice"}}
					<p>多选题</p>{{/if}} {{if v=="determine"}}
					<p>判断题</p>{{/if}} {{if v=="material"}}
					<p>材料题</p>{{/if}} {{/each}}
				</li>
				<li class="mui-table-view-cell">
					<h4>考试提醒:</h4>
					<p>您即将开始时长为{{if limitedtime=="0"}}无限制{{else}}{{limitedtime}}分钟{{/if}}的考试，请做好相关准备。</p>
					<p>若没有购买题集，需要先购买哦！</p>
					<p>点击『开始做题』即可开始考试。</p>
				</li>
			</ul>
		</script>

		<script src="../script/app.js" type="text/javascript" charset="utf-8"></script>
		<script src="../script/template.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			MYPAGE = {};
			template.helper('$parse', function(data) {
				return JSON.parse(data.replace(/\\\\/g, "\\"));
			});
			template.helper('$toObj', function(data) {
				return JSON.parse(data);
			});

			function success(ret) {
				var html = template('contentTem', ret.data);
				document.getElementById("content").innerHTML = html;
				hideWaiting();
			}

			function loadResulst(id) {
				showWaiting("稍等一下哦！");
				var key = getUserKey();
				var system = api.systemType;
				var data = {
					id: id,
					key: key,
					client:system
				}
				var url = MYAPP.webroot + "/api/index.php/Home/Index/getTestpaperInfo/";
				api.ajax({
					url: url,
					method: 'post',
					timeout: 30,
					dataType: 'json',
					data: {
						values: data
					},
					returnAll: false
				}, function(ret, err) {
					if (ret) {
						document.getElementById("newcourse").style.visibility="visible";
						success(ret);
						var status = ret.data.status;
						
						if (MYPAGE.data.type == "free") {
							document.getElementById("begin").style.width = "100%";
						} else {
							if (status == "unpurchased") {
								document.getElementById("buy").style.display = "block";
								document.getElementById("purchased").style.display = "none";
							} else if (status == "purchased") {
								document.getElementById("buy").style.display = "none";
								document.getElementById("purchased").style.display = "block";
							} else if (status == "free") {
								document.getElementById("begin").style.width = "100%";
							}
						}
						if (ret.data.limitedtime = 0) {
							localStorage.setItem('timer' + id, ret.data.limitedtime * 60);
						}
					} else {
						api.toast({
							msg: '错误码：' + err.code + '；错误信息：' + err.msg + '网络状态码：' + err.statusCode,
							location: 'middle',
							duration: 1000
						});
					}
				});
			}
			apiready = function() {
				var data = api.pageParam;
				loadResulst(data.id);
				MYPAGE.data = data;
				api.addEventListener({
					name: 'loginsuccess'
				}, function(ret, err) {
					loadResulst(MYPAGE.data.id);
				});
				api.addEventListener({
					name: 'paysuccess'
				}, function(ret, err) {
					api.closeWin();
				});
				document.getElementById("buy").addEventListener('tap', function() {
					showWaiting("稍等一下哦!");
					var testid = MYPAGE.data.id;
					var key = getUserKey();
					var system = api.systemType;
					var data = {
						testid: testid,
						key: key,
						client:system
					};
					var url = MYAPP.webroot + "/api/index.php/Home/Index/beginTestPaper";
					api.ajax({
						url: url,
						method: 'post',
						timeout: 30,
						dataType: 'json',
						data: {
							values: data
						},
						returnAll: false
					}, function(ret, err) {
						if (ret.code = 200) {
							hideWaiting();
							if (ret.status == "unpurchased") {
								var courseid = ret.courseid;
								api.confirm({
									title: '购买',
									msg: '您还没有购买该题集，是否购买?',
									buttons: ['确定', '取消']
								}, function(ret, err) {
									if (ret.buttonIndex == 1) {
										//处理重做事务
										api.openWin({
											name: 'buy',
											url: 'buy.html',
											pageParam: {
												id: courseid
											}
										});
									}
								});
							} else {
								api.toast({
									msg: ret.error
								});
								if (ret.error == "登录超时！") {
									login();
								}
							}
						} else {
							api.toast({
								msg: '错误码：' + err.code + '；错误信息：' + err.msg + '网络状态码：' + err.statusCode,
								location: 'middle',
								duration: 1000
							});
						}
					});
				});
				document.getElementById("begin").addEventListener('tap', function() {
					showWaiting("稍等一下哦!");
					var testid = MYPAGE.data.id;
					var key = getUserKey();
					var system = api.systemType;
					var data = {
						testid: testid,
						key: key,
						client:system
					}
					var url = MYAPP.webroot + "/api/index.php/Home/Index/beginTestPaper";
					api.ajax({
						url: url,
						method: 'post',
						timeout: 30,
						dataType: 'json',
						data: {
							values: data
						},
						returnAll: false
					}, function(ret, err) {
						if (ret.code = 200) {
							hideWaiting();
							if (ret.status == "finished") {
								api.confirm({
									title: '是否重做?',
									msg: '该试卷您已做答过，您可以重做一遍或者查看考试结果。',
									buttons: ['重做一遍', '查看结果', '取消']
								}, function(ret, err) {
									if (ret.buttonIndex == 1) {
										//处理重做事务
										api.openWin({
											name: 'test',
											url: 'test.html',
											pageParam: MYPAGE.data
										});
									} else if (ret.buttonIndex == 2) {
										var data = {
											id: MYPAGE.data.id
										}
										api.openWin({
											name: 'result',
											url: 'result.html',
											pageParam: data
										});
									}
								});
							} else if (ret.status == "ok" || ret.status == "doing") {
								api.openWin({
									name: 'test',
									url: 'test.html',
									pageParam: MYPAGE.data
								});
							} else if (ret.status == "unpurchased") {
								var courseid = ret.courseid;
								api.confirm({
									title: '购买',
									msg: '您还没有购买该题集，是否购买?',
									buttons: ['确定', '取消']
								}, function(ret, err) {
									if (ret.buttonIndex == 1) {
										//处理重做事务
										api.openWin({
											name: 'buy',
											url: 'buy.html',
											pageParam: {
												id: courseid
											}
										});
									}
								});
							} else {
								api.toast({
									msg: ret.error
								});
								if (ret.error == "登录超时！") {
									login();
								}
							}
						} else {
							api.toast({
								msg: '错误码：' + err.code + '；错误信息：' + err.msg + '网络状态码：' + err.statusCode,
								location: 'middle',
								duration: 1000
							});
						}
					});
				});
			};
		</script>
	</body>

</html>